package com.seabook.store.auth.web;

import javax.servlet.http.HttpServletRequest;


import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.seabook.store.auth.domain.Address;
import com.seabook.store.auth.domain.User;
import com.seabook.store.auth.service.UserService;
import com.seabook.store.util.ServiceLocator;

@Controller
public class AccountController {

    private UserService userService;

    public AccountController() {
        this.userService = (UserService) ServiceLocator.getBean("userService");
    }

    @RequestMapping(value = "account/account")
    public String showAccount(HttpServletRequest request) {
        User user = UserSession.getUserSession(request).getUser();
        user = userService.getUserInfo(user);
        request.setAttribute("user", user);
        return "account";
    }
    
    @RequestMapping(value = "account/manageaddress")
    public String showAddress(HttpServletRequest request) {
        User user = UserSession.getUserSession(request).getUser();
        user = userService.getUserInfo(user);
        UserSession.getUserSession(request).setUser(user);
        request.setAttribute("addresses", user.getReceiver());
        request.setAttribute("address", new Address());
        return "addressMgr";
    }
    
    @RequestMapping(value = "account/editaddress")
    public String editAddress(@RequestParam("addressId") int addressId, HttpServletRequest request){
        User user = UserSession.getUserSession(request).getUser();
        request.setAttribute("addresses", user.getReceiver());
        request.setAttribute("address", user.findAddress(addressId));
        return "addressMgr";
    }

    @RequestMapping(value = "account/saveaddress")
    public String saveAddress(Address address, BindingResult result, HttpServletRequest request) {
        User user = UserSession.getUserSession(request).getUser();
        new AddressValidator().validate(address, result);
        if (result.hasErrors()) {
        } else {
            address.setUserId(user.getUserId());
            address = this.userService.addAddress(address);
            user.updateAddress(address);
        }
        request.setAttribute("addresses", user.getReceiver());
        return "addressMgr";
    }
    
    @RequestMapping(value = "account/updateuser")
    public String updateUserInfo(User updateUser, HttpServletRequest request){
        User user = UserSession.getUserSession(request).getUser();
        Address address = updateUser.getDefaultAddress();
        address.setPhone1(updateUser.getPhone());
        address.setEmail(updateUser.getEmail());
        user.setDefaultReceiver(updateUser.getDefaultAddress());
        user.setEmail(updateUser.getEmail());
        user.setPhone(updateUser.getPhone());
        userService.saveUser(user);
        return "account";
    }
    
    @RequestMapping(value = "account/changepsw", method = RequestMethod.POST)
    public String changePsw(@RequestParam("password") String password, HttpServletRequest request){
        User user = UserSession.getUserSession(request).getUser();
        userService.changePassword( user.getUserId(), password);
        return "userinfo";
    }

}
